package com.service.xsxt;

import com.dao.xsxt.VoteInfoMapper;
import com.entity.common.ApiResult;
import com.entity.xsxt.RoomShow;
import com.entity.xsxt.VoteInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2019/3/7.
 */
@Service
public class VoteInfoService {

    @Autowired
    private VoteInfoMapper voteInfoMapper;

    public ApiResult insertSelective(VoteInfo record){
        voteInfoMapper.insertSelective(record);
        return null;
    }

    public ApiResult updateByPrimaryKeySelective(VoteInfo record){
        int num =voteInfoMapper.updateByPrimaryKeySelective(record);
        if(num>0){
            return new ApiResult(true,"更新成功!");
        }
        return new ApiResult(false,"更新失败！");
    }

    public ApiResult insertOrUpdate(VoteInfo record){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer integer = voteInfoMapper.countIp(record.getIpAddress(),sdf.format(new Date()));
        if(integer>=3){
            return new ApiResult(false,"投票失败！一个人每天最多可以投票三次");
        }
        int num=0;
        record.setVotes(1);
        record.setRoomId(record.getId());
        record.setCreateTime(new Date());
        record.setId(null);
        num=voteInfoMapper.insertSelective(record);
        if(num>0){
            return new ApiResult(true,"投票成功!");
        }
        return new ApiResult(false,"投票失败！");
    }

}
